<?php 

class convention_crt extends CI_Controller {  
       
	    function __construct()
    {
        parent::__construct();
         $this->load->library("pagination");
		$this->load->helper(array('form', 'url'));
        $this->load->library('Convert_ctr');
				

    }
	  
	 function index()
	{
		
		 $this->load->library('session');
		 $year=$this->session->userdata('annee');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('convention_mdl'); // load the file welcome_mdl.php
		$charticle = $this->charticle_mdl->get_all(); // call the method filtera inside the class welcome_mdl
	    $data_charticle = array('data_charticle'=>$charticle); //ajouter la table r1 a data		
	   $this->session->set_userdata($data_charticle); // mettre $data_charticle dans une var globale
	   
	    $config = array();
          $config["base_url"] = base_url( "convention_crt/index");
		  $config["total_rows"] =$this->convention_mdl->record_count_all();
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
 
        $this->pagination->initialize($config);
		 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
       $data["results"] = $this->convention_mdl->get_all($config["per_page"], $page);
	   $data["wilaya"] = $this->convention_mdl->get_wilaya();
	    $data["results_recherche"] = $this->convention_mdl->get_all_per_nom();//elle appaort tt les fournisseur pour rechercher leur convention
        $data["links"] = $this->pagination->create_links();
		$data['titre']='قائمة  الاتفاقيات الكلية';
		
		  $this->load->view('convention_vew',$data);  // send the result from the model to the view 
	
	}
	
	public function find_convention_per_fournis()
    {
		$this->load->model('convention_mdl'); // load the file welcome_mdl.php
		$v=$this->input->post('nom');
	   
	   
	    $config = array();
          $config["base_url"] = base_url( "convention_crt/find_convention_per_fournis");
		  $config["total_rows"] =$this->convention_mdl->record_count_recherche($v);
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
		  
		   $this->pagination->initialize($config);
		 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        
		  $data["wilaya"] = $this->convention_mdl->get_wilaya();
	    $data["results_recherche"] = $this->convention_mdl->get_all_per_nom();//elle appaort tt les fournisseur pour rechercher leur convention
        $data["links"] = $this->pagination->create_links();
	   $data["results"]= $this->convention_mdl->get_per_fournisseur($v);
	    $data['titre']=': الاتفاقيات الخاصة بالمورد <br> '.$v;
	    $this->load->view("convention_vew",$data);	
	   
    }
	
	public function find_convention_per_date()
    {
		$this->load->model('convention_mdl'); // load the file welcome_mdl.php
		$j1=$this->input->post('jour1');
		$j2=$this->input->post('jour2');
		$m1=$this->input->post('mois1');
		$m2=$this->input->post('mois2');
	   
	   
	   
      
	    $data["results_recherche"] = $this->convention_mdl->get_all_per_nom();//elle appaort tt les fournisseur pour rechercher leur convention
        $data["wilaya"] = $this->convention_mdl->get_wilaya();
	   $data["results"]= $this->convention_mdl->get_per_date($j1,$j2,$m1,$m2);
	    $data['titre']=': الاتفاقيات بالتاريخ <br> ';
		 $data['links']=' ';
	    $this->load->view("convention_vew",$data);	
	   
    }
	
	
	public function find_convention_per_wilaya()
    {
		$this->load->model('convention_mdl'); // load the file welcome_mdl.php
		$num_wilaya=$this->input->post('wilaya');
		
	   
	   
	   
      
	    $data["results_recherche"] = $this->convention_mdl->get_all_per_nom();//elle appaort tt les fournisseur pour rechercher leur convention
        $data["wilaya"] = $this->convention_mdl->get_wilaya();
	   $data["results"]= $this->convention_mdl->get_per_wilaya($num_wilaya);
	    $data['titre']=' الاتفاقيات الخاصة بالولاية : '.$num_wilaya.'<br> ';
		 $data['links']=' ';
	    $this->load->view("convention_vew",$data);	
	   
    }
	
	public function get_convention_per_article2($x1)
    {
		$this->load->model('convention_mdl'); // load the file welcome_mdl.php
	 $this->load->library('session');
	  $tab['val']=$x1;
	   $tab['nbr-enreg']= $this->convention_mdl->record_count($x1);
	   $tab['premier_appelle']=TRUE;
	  $this->session->set_userdata($tab); // mettre $tab dans une var globale
	  
	  

		
		$this->get_convention_per_article3();
		
	
	
    }
	
	public function get_convention_per_article3()
    {
		
	 $this->load->library('session');
        $x1=$this->session->userdata('val'); //access la var globale val

		$this->load->model('convention_mdl'); // load the file welcome_mdl.php
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		
			 /////pagination
		   $config = array();
         $config["base_url"] = base_url("convention_crt/get_convention_per_article3");
		  $config["total_rows"] =$this->convention_mdl->record_count($x1);
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
 //
       $this->pagination->initialize($config);
		  $premier=$this->session->userdata('premier_appelle'); //access la var globale val
	 if($premier){
	 	$page=0;
		 $tab['premier_appelle']=FALSE;
		  $this->session->set_userdata($tab);
		 }
	else $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		 
		
         $data["results"] = $this->convention_mdl->get_all2($config["per_page"], $page ,$x1);
		   $data["results_recherche"] = $this->convention_mdl->get_per_art($x1);//elle apport tt les fournisseur de article x1 pour rechercher leur convention
         $data["links"] = $this->pagination->create_links();
		   $data["wilaya"] = $this->convention_mdl->get_wilaya();
		 $tt=$data["results"];
		if($tt){
			$nom_article=$tt[0]->article;
			$data['titre']='قائمة  الاتفاقيات الخاصة بالمادة <br> '.$nom_article;
		}else{
			$art = $this->charticle_mdl->get_nom_article($x1);
			$data['titre']=': لا يوجد الاتفاقيات خاصة بالمادة  <br>'.$art[0]->nom ;
		}
	
		 
		 $this->load->view("convention_vew",$data);	
		
		
		
	
	
    }

    public function ajout_convention($id_bc,$id_fournisseur){
		$this->load->model('convention_mdl');
		$this->load->model('bc_mdl');
		$tmp=$this->convention_mdl->ajout_convention($id_fournisseur);// on cree un convention et recpere id convention
		$id_convention=$tmp[0]->id_convetion;
		$bcs=$this->bc_mdl->get_bc_per_fourn($id_fournisseur);
		foreach($bcs as $info){
			$this->bc_mdl->regularise_bc($info->id_bc,$id_convention);
		}
		echo "<script> alert(\" convention ajoute avec succes \")</script>";
		
		$this->get_detaille_convention($id_convention);
	$g=0;
	}

    public function supprimer_convention($id_convention){
		$this->load->model('convention_mdl');
		
		$tmp=$this->convention_mdl->supprimer_convention($id_convention);// on cree un convention et recpere id convention
		//$id_convention=$tmp[0]->id_convetion;
	
 $this->load->library('session');
		 $year=$this->session->userdata('annee');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		//$this->load->model('convention_mdl'); // load the file welcome_mdl.php
		$charticle = $this->charticle_mdl->get_all(); // call the method filtera inside the class welcome_mdl
	    $data_charticle = array('data_charticle'=>$charticle); //ajouter la table r1 a data		
	   $this->session->set_userdata($data_charticle); // mettre $data_charticle dans une var globale
	   
	    $config = array();
          $config["base_url"] = base_url( "convention_crt/index");
		  $config["total_rows"] =$this->convention_mdl->record_count_all();
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
 
        $this->pagination->initialize($config);
		 $page = 0;
       $data["results"] = $this->convention_mdl->get_all($config["per_page"], $page);
	    $data["results_recherche"] = $this->convention_mdl->get_all_per_nom();//elle appaort tt les fournisseur pour rechercher leur convention
        $data["links"] = $this->pagination->create_links();
		$data['titre']='قائمة  الاتفاقيات الكلية';
		
		  $this->load->view('convention_vew',$data);  // send the result from the model to the view 
	}
	
	  public function ajout_convention_regularise($id_bc,$id_fournisseur){
		$this->load->model('convention_mdl');
		$this->load->model('bc_mdl');
		
		$today = getdate();
				$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
		
		$tmp=$this->convention_mdl->ajout_convention($id_fournisseur,$date);// on cree une convention et recupere id convention
		$id_convention=$tmp[0]->id_convetion;
		$bcs=$this->bc_mdl->get_bc_per_fourn($id_fournisseur);
		foreach($bcs as $info){
			$this->bc_mdl->regularise_bc($info->id_bc,$id_convention);
		}
		//echo "<script> alert(\" convention ajoute avec succes \")</script>";
		$rmq=0;
		$regularise=1;
		$this->get_detaille_convention($id_convention,$rmq,$regularise);
	$g=0;
	}
	
	 public function detaille_convention_regularise($id_convention,$msg){
	
		$this->load->model('convention_mdl');
		$this->load->model('bc_mdl');
				
		$tmp=$this->convention_mdl->get_convention_detaille($id_convention);
		//$tmp2=$this->convention_mdl->get_convention_montant($id_convention);
		//$bcs=$this->convention_mdl->get_convention_bcs($id_convention);
	    $bcs_id=$this->convention_mdl->get_convention_bcs_id($id_convention);
		
			$tab_bcs=NULL;
		for($i=0;$i<count($bcs_id);$i++){
			$tab_bcs[$i]=$this->bc_mdl->get_detaille($bcs_id[$i]->id_bc);
		}
		/*$montant=0;
		foreach($tmp2 as $info){
			$montant=$montant+($info->quantite*$info->bc_produit_pu)+(($info->quantite*$info->bc_produit_pu)*$info->tva/100);
		}
		$tab=NULL;
		foreach($bcs as $datas){
			$cle=$datas->id_bc;
			$tmp2=$this->convention_mdl->get_prix_prod_bcs($cle);
			$mont=0;
			foreach($tmp2 as $info){
				$mont=$mont +($info->bc_produit_pu* $info->quantite)+(($info->bc_produit_pu* $info->quantite)*$info->tva/100);
			}
			$tab[$cle]=$mont;
		}*/
		//$lettre=$this->convert_ctr->convert_number($montant);
		$data['result']=$tmp;
		$data['tab_bcs']=$tab_bcs;
		//$data['lettre']=$lettre;
		//$data['montant']=$montant;
		//$data['bcs']=$bcs;
		//$data['montant_bcs']=$tab;
		$data['msg']=$msg;
		 $this->load->view('convention_regularise_detaille_vew',$data); 
	}
	
	
	 public function imprimer_convention_regularise($id_convention){
	
		$this->load->model('convention_mdl');
		$this->load->model('fournisseur_mdl2');
		$this->load->model('bc_mdl');
				
		$tmp=$this->convention_mdl->get_convention_detaille($id_convention);
		$tmp2=$this->convention_mdl->get_convention_montant($id_convention);
		$bcs_id=$this->convention_mdl->get_convention_bcs_id($id_convention);
	
		$tab_bcs=NULL;
		for($i=0;$i<count($bcs_id);$i++){
			$tab_bcs[$i]=$this->bc_mdl->get_detaille($bcs_id[$i]->id_bc);
		}
		
		$id_fournisseur=$tmp[0]->id_fournisseur;
		$data['detaille_fournisseur'] = $this->fournisseur_mdl2->get_detaille_fournisseur($id_fournisseur);
		$data['result']=$tmp;
		$data['tab_bcs']=$tab_bcs;
		
		 $this->load->view('convention_regule_imprimer_vew',$data); 
	}
	
	 public function update_convention($id)
    { 
	
		$delai_realisation= $this->input->post('delai_realisation');
		$date= $this->input->post('date');
		$delai_payement= $this->input->post('delai_payement');
		

		
		$tab['delai_realisation']=$delai_realisation;
		$tab['date']=$date;
		$tab['delai_payement']=$delai_payement;
		
		
		if (!(preg_match('/\\d{4}-\\d{2}-\\d{2}/', $date)))
		{
			 $errors['date'] = TRUE;
		}
		if (!(preg_match('/\\d+/', $delai_realisation)))
		{
			 $errors['delai_realisation'] = TRUE;
		}
		if (!(preg_match('/\\d+/', $delai_payement)))
		{
			 $errors['delai_payement'] = TRUE;
		}
		
		if (empty($errors)) { 
			$this->load->model('convention_mdl');
			$this->convention_mdl->update_convention($id,$delai_realisation,$delai_payement,$date); // get  one fournisseur per id
			
		     $this->load->view('convention_maj_success_vew');   // send the result from the model to the vie
			//$this->index();
		
		}
		else{
			// do not update
			$mm = 1;
			$this->detaille_convention($id,$errors);
		}
		$vv=1; 
	}

      public function modifier_convention($id)
    { 
	
		$numero= $this->input->post('numero');
		$date= $this->input->post('date');
		
		
		
		if (!(preg_match('/\\d{4}-\\d{2}-\\d{2}/', $date)))
		{
			 $errors['date'] = TRUE;
		}
		if (!(preg_match('/\\d+/', $numero)))
		{
			 $errors['numero'] = TRUE;
		}
		
		$rgularise=0;
		if (empty($errors)) { 
			$this->load->model('convention_mdl');
			$this->convention_mdl->modifier_convention($id,$numero,$date); // get  one fournisseur per id
			
		    // $this->load->view('convention_maj_success_vew');   // send the result from the model to the vie
			//$this->index();
		$mm = 0;
			$this->get_detaille_convention($id,$mm,$rgularise);
		}
		else{
			// do not update
			$mm = 1;
			$this->get_detaille_convention($id,$mm,$rgularise);
		}
		$vv=1; 
	}

     public function ajout_convention_new($msg)
    { 
	    $this->load->model('fournisseur_mdl2'); // load 
		$this->load->model('charticle_mdl'); // load 
		
	    $result['fournisseurs'] = $this->fournisseur_mdl2->get_all();   // get all  for autocomplete
	    $result['charticles'] = $this->charticle_mdl->get_all();   // get all  for autocomplete
		$result['message']=$msg;
	    if(is_array($msg)){
			
	    if(array_key_exists('0',$msg)){
			$result['nom_fourn']=$msg[0]->nom;
		}
		}else $result['nom_fourn']=NULL;
		$this->load->view('convention_ajout_vew',$result); 
		
	}
	
		public function valider_ajout_convention()
    {
		$fournisseur= $this->input->post('fournisseur');
		$article= $this->input->post('article');
		$tva= $this->input->post('tva');
		$numero= $this->input->post('numero');
		//$consultation= $this->input->post('consultation');
		
		$this->load->model('engagement_mdl'); // load 
		$this->load->model('convention_mdl'); // load 
		$this->load->model('fournisseur_mdl2'); // load
		$this->load->model('charticle_mdl'); // load  
		    if($fournisseur==NULL OR $article==NULL){
				$fourniss=NULL;
				$id_article=NULL;
			}else{
			$fourniss = $this->fournisseur_mdl2->get_id($fournisseur);
			$id_article = $this->charticle_mdl->get_id_article($article);
			}
		
		$existe_numero=FALSE;
		if($numero){
		$numero_bon=$this->convention_mdl->existe_numero_convention($numero);	//test si le numero de convention existe deja
		if($numero_bon!=0)$existe_numero=TRUE;
		}else{
			$num=$this->convention_mdl->new_numero_convention();
			if($num)$numero=$num[0]->numero+1;
			else $numero=1;
		}
		
		
		
		if($fourniss and $id_article and !$existe_numero){
			
			$id_art=$id_article[0]->id_article;
			$id_fournisseur=$fourniss[0]->id_fournisseur;
			$rmq=0;
			$today = getdate();
				$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
			   $id_engagement=NULL;
				$this->convention_mdl->ajout_convention_normale($id_fournisseur,$id_engagement,$date,$numero,$id_art,$tva);
				$new_id=$this->convention_mdl->get_convention_nouveau_id();
				$id=$new_id[0]->id_convetion;
				$rmq=0;
				$regularise=0;
				$this->get_detaille_convention($id,$rmq,$regularise);
				
		}else{
			$msg='هناك خطأ في تعبئة الاتفاقية';
			$this->ajout_convention_new($msg);
		}		
				//////////////
				
		/*if($id_article and $fourniss){
			$id_art=$id_article[0]->id_article;
			if($num_engagement==''){
			
		     
			$today = getdate();
				$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
				$id_fournisseur=$fourniss[0]->id_fournisseur;
				$id_engagement='';
				$this->convention_mdl->ajout_convention_normale($id_fournisseur,$id_engagement,$date,$numero,$id_art);
				$new_id=$this->convention_mdl->get_convention_nouveau_id();
				$id=$new_id[0]->id_convetion;
				$rmq=0;
				$regularise=0;
				$this->get_detaille_convention($id,$rmq,$regularise);
				//$this->index();
			
		}elseif(!(preg_match('/\\d+/', $num_engagement)))
		{
			 $errors['num_engagement'] = TRUE;
			 $msg='erreur';
			 $this->ajout_convention_new($msg);
		}else{
					
		$engagement = $this->engagement_mdl->verifier_num_enga($article,$num_engagement);
		$fourniss = $this->fournisseur_mdl2->get_id($fournisseur);
		$nature=-1;
		if($engagement){ // si engagement existe
			$nature=$engagement[0]->nature;
			if($nature==1){ // si engagement de nature depence ok on ajoute le bc
				$today = getdate();
				$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
				$id_engagement=$engagement[0]->id_engagement;
				$id_fournisseur=$fourniss[0]->id_fournisseur;
				$this->convention_mdl->ajout_convention_normale($id_fournisseur,$id_engagement,$date,$numero,$id_art);
				$new_id=$this->convention_mdl->get_convention_nouveau_id();
				$id=$new_id[0]->id_convetion;
				$rmq=0;
				$regularise=0;
				$this->get_detaille_convention($id,$rmq,$regularise);
				//$this->index();
			}
			else{ // si engagement de nature epargne
				echo "<script>alert(\"طبيعة هذا الالتزام توفير\")</script>"; 
				$msg='';
				$this->ajout_convention_new($msg);
			}
		}
		else{ // si pas engagement
			$nature=0;
			
			echo "<script> var reponse =confirm(\"لا يوجد التزام بهذا الرقم\")</script>"; 
			$msg=1;
			$this->ajout_convention_new($msg);
		}
		$m=0;
		}
		}
		else{
			$msg='حقل ادخال المادة خاطئ';
				$this->ajout_convention_new($msg);
		}  */
    }	
		
	public function get_detaille_convention($id,$rmq,$regularise)
    {
		if($regularise==1){
			$this->detaille_convention_regularise($id,$rmq);
		}else{
			
		$this->load->model('convention_mdl'); // load the file welcome_mdl.php
		$this->load->model('fournisseur_mdl2'); // load 
		$this->load->model('produit_mdl'); // load  
		
		$result['les_convention'] = $this->convention_mdl->get_convention_detaille($id);
	    $result['fournisseurs'] = $this->fournisseur_mdl2->get_all();   // get all  for autocomplete
		$result['produits'] = $this->produit_mdl->get_dessignation();
		$result['rmq']=$rmq;
		$tmp=$result['les_convention'];
		$montant=0;
		if(array_key_exists('quantite',$tmp[0])){
		   foreach($tmp as $data) { 
           $montant=$montant +($data->quantite * $data->pu_convention_produit)+(($data->quantite * $data->pu_convention_produit)*$data->tva/100);
	        }
			
		}  
		$this->convention_mdl->set_montant($id,$montant);
		$existe_annex=$this->convention_mdl->existe_annex($id);
		
		     $lettre=$this->convert_ctr->convert_number($montant);
			 $result['existe_annex']=$existe_annex;
			$result['lettre']=$lettre;
            $result['montant'] =$montant;
			$this->load->view('convention_detaille_vew2',$result); 
    }
}

   
     public function modifier_prod($i,$id,$id_convention_prod,$id_fournisseur)
    { 
	    $rmq=0; // var pr le besoin de convention de regularisation
		$dessignation= $this->input->post('dessignation'.$i);
		$prix= $this->input->post('prix'.$i);
		$quantite= $this->input->post('quantite'.$i);
		$pu= $this->input->post('pu'.$i);
		$regularise=0;
		$this->load->model('produit_mdl'); // load 
		
		$tab['dessignation']=$dessignation;
		$tab['id_convention']=$id;
		$tab['prix']=$prix;
		$tab['quantite']=$quantite;	
		$tab['pu']=$pu;	
		if($dessignation==""){ // si le champs est vide on supprime le prod
			$result = $this->produit_mdl->supprime_produit_convention($id_bc_prod);
			$this->get_detaille_convention($id,$rmq,$regularise);
			//$this->verifie_seuill_fournisseur($id_fournisseur,$id);
		}else{ // sinon on modifier le prod
		    $produit = $this->produit_mdl->get_prix($dessignation);
		//on va verifie le prix
		    if(!$produit){ //test si le produit existe deja
		    $this->produit_mdl->ajout_nouveau_produit($dessignation,$prix);
			$produit = $this->produit_mdl->get_prix($dessignation);
			}
			$tab['id_prod']=$produit[0]->id_produit;
			$result = $this->produit_mdl->modifier_produit_convention($id_convention_prod,$tab);
			$this->get_detaille_convention($id,$rmq,$regularise);
			//$this->verifie_seuill_fournisseur($id_fournisseur,$id);
		}
	
		
		}	
		   
    public function ajout_produit($id,$id_fournisseur)
    {
		
		$this->load->model('produit_mdl'); // load 
		$result['produit'] = $this->produit_mdl->get_dessignation();
		$result['id_convention']=$id;
		$result['id_fournisseur']=$id_fournisseur;
		$this->load->view('convention_ajout_produit_vew',$result); 
		
    }
	
	

     public function imprimer($id)
    {
		$this->load->model('convention_mdl'); // load 
		$this->load->model('produit_mdl'); // load 
		$this->load->model('fournisseur_mdl2'); // load 
		
		$result['les_convention'] = $this->convention_mdl->get_convention_detaille($id);
		$result['produit'] = $this->produit_mdl->get_dessignation();
		
		
		$tmp=$result['les_convention'];
		$id_fournisseur=$tmp[0]->id_fournisseur;
		$result['detaille_fournisseur'] = $this->fournisseur_mdl2->get_detaille_fournisseur($id_fournisseur);
		$montant=0;
		if(array_key_exists('quantite',$tmp[0])){
		   foreach($tmp as $data) { 
           $montant=$montant +($data->quantite * $data->pu_convention_produit);
	        }
			
		}  
		     $lettre=$this->convert_ctr->convert_number($montant);
			$result['lettre']=$lettre;
            $result['montant'] =$montant;
		$this->load->view('convention_detaille_imprimer_vew',$result); 
		
    }
	
	public function valider_ajout_prod($id,$id_fournisseur)
    {
		$rmq=0;
		$dessignation= $this->input->post('dessignation');
		$prix= $this->input->post('prix');
		$quantite= $this->input->post('quantite');
		$this->load->model('produit_mdl'); // load 
		$produit = $this->produit_mdl->get_prix($dessignation);
		$regularise=0;
		//on va verifie le prix
		if($produit){ //test si le produit existe deja
		$prix_originale=$produit[0]->prix_u;
		if($prix<=$prix_originale){ //test si le produit est chere
		$tab['dessignation']=$dessignation;
		$tab['id_convention']=$id;
		$tab['prix']=$prix;
		$tab['quantite']=$quantite;	
		$tab['prix_orig']=$prix_originale;
		$result = $this->produit_mdl->ajout_produit_convention($tab);
		
		// $this->get_detaille_bc2($id);  // send the result from the model to the view 
		 $this->get_detaille_convention($id,$rmq,$regularise);
		}else{
			// prix non raissonable
			$tab['dessignation']=$dessignation;
			$tab['prix']=$prix;
			$tab['prix_original']=$prix_originale;
			$tab['quantite']=$quantite;
			$tab['id_convention']=$id;
			$tab['id_fournisseur']=$id_fournisseur;
			$this->load->view('convention_produit_ajout_chere_vew',$tab); 
			
		}
		}else{// on cree nouveau produit
		$this->produit_mdl->ajout_nouveau_produit($dessignation,$prix);
		$tab['dessignation']=$dessignation;
		$tab['id_convention']=$id;
		$tab['prix']=$prix;
		$tab['quantite']=$quantite;	
		$result = $this->produit_mdl->ajout_produit_convention($tab);
		// $this->get_detaille_bc2($id); 
	     $this->get_detaille_convention($id,$rmq,$regularise);
		}
		$d=0;
    }
    
	
	
       public function valider_ajout_prod_chere($id,$id_fournisseur)
    {
		$rmq=0;
		$dessignation= $this->input->post('dessignation');
		$prix= $this->input->post('prix');
		$quantite= $this->input->post('quantite');
		$this->load->model('produit_mdl'); // load 
		$produit = $this->produit_mdl->get_prix($dessignation);
		$regularise=0;
		//on va verifie le prix
		if($produit){ //test si le produit existe deja
		$prix_originale=$produit[0]->prix_u;
		
		$tab['dessignation']=$dessignation;
		$tab['id_convention']=$id;
		$tab['prix']=$prix;
		$tab['quantite']=$quantite;	
		$tab['prix_orig']=$prix_originale;
		$result = $this->produit_mdl->ajout_produit_convention($tab);
		
		// $this->get_detaille_bc2($id);  // send the result from the model to the view 
		 $this->get_detaille_convention($id,$rmq,$regularise);
		
		}else{// on cree nouveau produit
		$this->produit_mdl->ajout_nouveau_produit($dessignation,$prix);
		$tab['dessignation']=$dessignation;
		$tab['id_bc']=$id;
		$tab['prix']=$prix;
		$tab['quantite']=$quantite;	
		$result = $this->produit_mdl->ajout_produit_bc($tab);
		// $this->get_detaille_bc2($id); 
	     $this->get_detaille_convention($id,$rmq,$regularise);
		}
		$d=0;
		}		
		
     public function supprime_prod($id_convention_prod,$id,$id_fournisseur)
        { 		
		$rmq=0;
		$regularise=0;
		$this->load->model('produit_mdl'); // load 
		$result = $this->produit_mdl->supprime_produit_convention($id_convention_prod);
		 $this->get_detaille_convention($id,$rmq,$regularise);
		
		
		}
		   
	public function mise_a_jour_convention($id_convention,$id_article)
        { 		
		$this->load->model('convention_mdl'); // load 
		$this->load->model('engagement_mdl'); // load 
		
		$num_enga=$this->engagement_mdl->get_engagement_prec($id_article);
		
		if(!$num_enga){
			$regularise=0;
			$rmq=0;
			$this->get_detaille_convention($id_convention,$rmq,$regularise);
		}else{
			$conv=$this->convention_mdl->get_convention_detaille_mini($id_convention);
			
			if($num_enga[0]->nature==1){// si nature est depense
				$montant_prec=$num_enga[0]->montant_precedent-$num_enga[0]->montant_engagement;
			}else{
				$montant_prec=$num_enga[0]->montant_engagement+$num_enga[0]->montant_precedent;
			}
			
			$montant=$conv[0]->montant;
			
			$numero=$num_enga[0]->numero+1;
			$nature=1;
			$today = getdate();
			$time=$today[0];
			$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
			$this->engagement_mdl->ajout_engagement3($id_article,$nature,$date,$numero,$time,$montant,$montant_prec);
			$num_enga=$this->engagement_mdl->get_engagement_numero($id_article);
			$id_engagement=$num_enga[0]->id_engagement;
			$this->convention_mdl->engager_convention($id_convention,$id_engagement);
		$rmq=0;
		$regularise=0;
		//$engagement= $this->input->post('engagement');

	$this->get_detaille_convention($id_convention,$rmq,$regularise);
	//	}
		
		
		}
		   
  
   }
			   
    public function payer_convention($id_convention,$id_fournisseur,$montant)
        { 		
		$this->load->model('convention_mdl'); // load 
		$this->load->model('mondat_mdl'); // load 
		
		$num_cheque= $this->input->post('num_cheque');
		$num_facture= $this->input->post('num_facture');
		$today = getdate();
		$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
		$num_mondat=$this->mondat_mdl->get_num_mondat();
		if($num_mondat){
			$numero_mondat=$num_mondat[0]->numero;
		}else{
			$numero_mondat=1;
		}
		$result = $this->convention_mdl->payer_convention($num_cheque,$num_facture,$date,$numero_mondat,$montant);
		$id_mondat=$result[0]->id_mandat;
		$this->convention_mdl->attacher_convention_mondat($id_convention,$id_mondat);
		$rmq=0;
		$regularise=0;
		$this->get_detaille_convention($id_convention,$rmq,$regularise);
		
		}
		   
	public function ajout_fournisseur_pour_convention($tab,$errors){
		
		$this->load->model('fournisseur_mdl2');
		$this->load->model('activite_mdl');
		$this->load->model('banque_mdl');
		
		
		$data2['rec'] = $this->fournisseur_mdl2->get_all();   // get all  for autocomplete
		$data3['rec'] = $this->activite_mdl->get_all();  //get all activity list for autocomplete 
		$data4['rec'] = $this->banque_mdl->get_all_banques(); // get the bank details for the fournisseur

		$r['liste_fourn']    = $data2;
		$r['liste_activite'] = $data3;
		$r['liste_banques'] = $data4;
		$r['errors']=$errors;
        $r['tab']=$tab;
		
		$this->load->view('fournisseur_ajout_vew3',$r); 
	}	
	
	public function valider_ajout_fournisseur_pour_convention()
	{
		//$this->load->model('banque_mdl');
		
		$errors = array();
		// check for data concistency
		$phone= $this->input->post('phone');
		//$mobile= $this->input->post('mobile');
		$rc= $this->input->post('rc');
		$rib= $this->input->post('rib');
		$email= $this->input->post('email'); 
		$numero= $this->input->post('numero');
		$fax= $this->input->post('fax');
		$num_fiscale= $this->input->post('num_fiscale'); 
		$nom= $this->input->post('nom'); 
		$address= $this->input->post('address'); 
		$type_activit= $this->input->post('type_activit');
		$wilaya= $this->input->post('wilaya'); 
		$banque= $this->input->post('banque'); 
		$adress_banque= $this->input->post('adress_banque'); 
		
		$this->load->model('activite_mdl');
		$data = $this->activite_mdl->get_activite_per_nom($type_activit);  //get all activity list for autocomplete 
		if($data) $id_activite=$data[0]->id_activite;
		else $errors['type_activit'] = TRUE;
		
		/*if($banque){
			
		$data_banque = $this->banque_mdl->get_id_banque($banque);  
			if($data_banque){// la banque existe deja
			$id_banque=$data_banque[0]->id_banque;
		}else{ // si la banque ne existe pas oncree une
			$la_banque = $this->banque_mdl->ajout_banques($banque); 
			$id_banque=$la_banque[0]->id_banque;
		}
		}else  $errors['banque'] = TRUE;
		*/
		
		if (! $nom)
		{
			 $errors['nom'] = TRUE;
		}
		
		if (! $wilaya)
		{
			 $errors['wilaya'] = TRUE;
		}
		
		if (! $address)
		{
			 $errors['address'] = TRUE;
		}
		
		if (! $phone)
		{
			 $errors['phone'] = TRUE;
		}
		
		if (! $fax)
		{
			 $errors['fax'] = TRUE;
		}
		
		if (! $adress_banque)
		{
			 $errors['adress_banque'] = TRUE;
		}
		
		if (!$num_fiscale)
		{
			 $errors['num_fiscale'] = TRUE;
		}
		/*if (!(preg_match('/\\(0(5|6|7)\\d{2}\\)-\\d{2}-\\d{2}-\\d{2}/', $mobile)))
		{
			 $errors['mobile'] = TRUE;
		}*/
		if (! $rc)
		{
			 $errors['rc'] = TRUE;
		}
		if (! $rib)
		{
			 $errors['rib'] = TRUE;
		}
		if($email != FALSE) // if email was typed check it's syntax
		{
			if (!(preg_match('/[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}/', $email)))
			{
				 $errors['email'] = TRUE;
			}
		}
		
		/// if no errors do update
		if (empty($errors)) { 
		    $this->load->model('fournisseur_mdl2');
			
			if($numero){// si le numero fournisseur est entree
				
			$existe_numero=$this->fournisseur_mdl2->test_numero($numero);
			// on test si le numero existe
			if($existe_numero) $errors['numero'] = TRUE;
			 // get  one fournisseur per id
			
			}else{// si numero non entree on cree un
				$tab_num=$this->fournisseur_mdl2->get_new_number();
				if($tab_num){
					$numero=$tab_num[0]->numero+1;
				}else $numero=1;
			}
			$tmp=$this->fournisseur_mdl2->ajout_fournisseur_new($nom,$phone,$fax,$rc,$rib,$email,$address,$id_activite,$numero,$wilaya,$num_fiscale,$banque,$adress_banque);
			$id=$tmp[0]->id_fournisseur;
			//$this->banque_mdl->ajout_banques_fournisseur($id_banque,$id);
			
			$this->ajout_convention_new($tmp);
		
		}
		else{
			// do not update
			echo"<script> alert(\"هناك حقل ادخال غير مطابق\")</script>"; 
			$tab['nom']=$nom;
			$tab['phone']=$phone;
			$tab['numero']=$numero;
			$tab['fax']=$fax;
			$tab['address']=$address;
			$tab['rc']=$rc;
			$tab['num_fiscale']=$num_fiscale;
			$tab['rib']=$rib;
			$tab['type_activit']=$type_activit;
			$tab['email']=$email;
			$tab['wilaya']=$wilaya;
			$tab['banque']=$banque;
			$tab['adress_banque']=$adress_banque;
			$this->ajout_fournisseur_pour_convention($tab,$errors);
		}
		$vv=1; 
	}		
}
